//
//  RBMyCoinChargeVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/8/3.
//

#import "RBMyCoinChargeVC.h"
#import "UIView+RBAdd.h"
#import "UIScrollView+RBAdd.h"
#import <Masonry.h>
#import "RBThemeConfig.h"
#import <UIColor+YYAdd.h>
#import <UIView+YYAdd.h>
#import "RBPager.h"
#import <UINavigationController+FDFullscreenPopGesture.h>
#import "RanTool.h"
#import "UIViewController+RBAdd.h"

@interface RBMyCoinChargeVC ()

@property(nonatomic, strong)UIScrollView *scrollView;
@property(nonatomic, strong)NSMutableArray *amountViews;
@property(nonatomic, assign)NSInteger amountIndex;
@property(nonatomic, strong)NSMutableArray *payWayViews;
@property(nonatomic, assign)NSInteger payWayIndex;
@property(nonatomic, strong)UILabel *noteL;

@end

@implementation RBMyCoinChargeVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
    [self fillData];
}

#pragma mark - Private

- (void)setup
{
    self.title = @"充值";
}

- (void)fillData
{
    [self.scrollView rbSetContentWidth:kRBMainScreenWidth];
    
    [self.scrollView addView:({
        UIView *view = UIView.new;
        
        UILabel *leftL = [RanTool labelWithFontSize:Zoom(16) weight:1 color:[RBThemeConfig textBlackColor] align:0 text:@"请选择充值金额："];
        [view addSubview:leftL];
        [leftL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(Zoom(16));
            make.top.bottom.mas_equalTo(0);
        }];
        
        view;
    }) withHeight:CGFLOAT_MIN width:kRBMainScreenWidth top:Zoom(19) bottom:0 left:0 right:0 centerX:0];
    [self.scrollView addView:({
        UIView *view = UIView.new;
        
        CGFloat itemW = Zoom(110);
        CGFloat itemH = Zoom(62);
        CGFloat xPadding = Zoom(6);
        CGFloat yPadding = Zoom(10);
        CGFloat totalH = 0;
        for (NSInteger i = 0; i < 7; i++)
        {
            NSInteger row = i/3;
            NSInteger column = i%3;
            CGFloat x = column*(itemW + xPadding);
            CGFloat y = row*(itemH + yPadding);
            UIView *amountView = [self amountViewWithAmount:@"100" discount:@"99.99"];
            amountView.tag = i;
            [amountView rbAddTapGesWithTarget:self action:@selector(amountViewTapped:)];
            [view addSubview:amountView];
            amountView.frame = CGRectMake(x, y, itemW, itemH);
            [self.amountViews addObject:amountView];
            totalH = y + itemH;
        }
        
        UIView *pview = UIView.new;
        pview.backgroundColor = UIColor.clearColor;
        [view addSubview:pview];
        [pview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.mas_equalTo(0);
            make.width.mas_equalTo(1);
            make.height.mas_equalTo(totalH);
        }];
        
        view;
    }) withHeight:CGFLOAT_MIN width:kRBMainScreenWidth - Zoom(16) top:Zoom(15) bottom:0 left:Zoom(16) right:0 centerX:0];
    [self.scrollView addView:({
        UIView *view = UIView.new;
        
        UILabel *leftL = [RanTool labelWithFontSize:Zoom(16) weight:1 color:[RBThemeConfig textBlackColor] align:0 text:@"支付方式："];
        [view addSubview:leftL];
        [leftL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(Zoom(16));
            make.top.bottom.mas_equalTo(0);
        }];
        
        view;
    }) withHeight:CGFLOAT_MIN width:kRBMainScreenWidth top:Zoom(36) bottom:0 left:0 right:0 centerX:0];
    for(NSInteger i = 0; i < 2; i++)
    {
        [self.scrollView addView:[self payWayWithIndex:i] withHeight:Zoom(50) width:kRBMainScreenWidth top:0 bottom:0 left:0 right:0 centerX:0];
    }
    [self.scrollView addView:({
        UIView *view = UIView.new;
        UILabel *titleL = [RanTool labelWithFontSize:Zoom(16) weight:1 color:[RBThemeConfig textBlackColor] align:0 text:@"注意事项"];
        [view view_addView:titleL withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:CGFLOAT_MIN left:Zoom(0) right:CGFLOAT_MIN centerX:0];
        [view view_addView:self.noteL withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(20) bottom:Zoom(30) left:Zoom(0) right:Zoom(0) centerX:0];
        
        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(16) bottom:0 left:Zoom(16) right:Zoom(16) centerX:NO];
    [self.scrollView addView:({
        UILabel *view = [RanTool labelWithFontSize:15 weight:0 color:[UIColor whiteColor] align:1 text:@"立即充值"];
        view.backgroundColor = [UIColor colorWithHexString:@"#518CFF"];
        [view rbSetCornerRadius:Zoom(22)];
        view;
    }) withHeight:Zoom(44) width:Zoom(350) top:Zoom(88) bottom:Zoom(50) left:CGFLOAT_MIN right:CGFLOAT_MIN centerX:YES];
    [self selectAmountAtIndex:0];
    [self selectPayWayAtIndex:0];
}

- (void)selectAmountAtIndex: (NSInteger)index
{
    self.amountIndex = index;
    for (UIView *itemView in self.amountViews)
    {
        if(itemView.tag == index)
        {
            itemView.backgroundColor = [UIColor colorWithHexString:@"#FFEBEA"];
            [itemView rbSetBorderWitdh:Zoom(1) borderColor:[UIColor colorWithHexString:@"#F25B50"]];
            [itemView viewWithTag:101].hidden = NO;
            [itemView viewWithTag:102].hidden = YES;
        }
        else
        {
            itemView.backgroundColor = UIColor.whiteColor;
            [itemView rbSetBorderWitdh:Zoom(1) borderColor:[UIColor colorWithHexString:@"#F2F2F2"]];
            [itemView viewWithTag:102].hidden = NO;
            [itemView viewWithTag:101].hidden = YES;
        }
    }
}

- (void)selectPayWayAtIndex: (NSInteger)index
{
    self.payWayIndex = index;
    for (UIView *itemView in self.payWayViews)
    {
        UIImageView *imgView = (UIImageView *)[itemView viewWithTag:33];
        imgView.image = itemView.tag == index? [UIImage imageNamed:@"groupPayWallet"]: [UIImage imageNamed:@"groupPayUnsel"];
    }
}

#pragma mark - Network

#pragma mark - Event

- (void)amountViewTapped: (UITapGestureRecognizer *)ges
{
    [self selectAmountAtIndex:ges.view.tag];
}

- (void)payWayViewTapped: (UITapGestureRecognizer *)ges
{
    [self selectPayWayAtIndex:ges.view.tag];
}

#pragma mark - Setter

#pragma mark - Getter

- (UIScrollView *)scrollView
{
    if (!_scrollView)
    {
        self.scrollView = ({
            UIScrollView *view = UIScrollView.new;
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.mas_equalTo(0);
            }];
            view;
        });
    }
    return _scrollView;
}

- (UIView *)amountViewWithAmount: (NSString *)amount discount: (NSString *)discount
{
    UIView *view = UIView.new;
    [view rbSetCornerRadius:Zoom(8)];
    
    UILabel *topL = UILabel.new;
    topL.tag = 101;
    topL.attributedText = [RanTool attributeStringMaker:^(NSMutableArray *maker) {
        [maker addAttributeStringWithFont:[UIFont boldSystemFontOfSize:Zoom(18)] color:[UIColor colorWithHexString:@"#F25B50"] string:amount];
        [maker addAttributeStringWithFont:[UIFont boldSystemFontOfSize:Zoom(11)] color:[UIColor colorWithHexString:@"#F25B50"] string:@"枚"];
    }];
    [view addSubview:topL];
    [topL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(Zoom(14));
    }];
    
    UILabel *topL1 = UILabel.new;
    topL1.tag = 102;
    topL1.attributedText = [RanTool attributeStringMaker:^(NSMutableArray *maker) {
        [maker addAttributeStringWithFont:[UIFont boldSystemFontOfSize:Zoom(18)] color:[UIColor colorWithHexString:@"#333333"] string:amount];
        [maker addAttributeStringWithFont:[UIFont boldSystemFontOfSize:Zoom(11)] color:[UIColor colorWithHexString:@"#333333"] string:@"枚"];
    }];
    [view addSubview:topL1];
    [topL1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(Zoom(14));
    }];
    
    UILabel *bottomL = [RanTool labelWithFontSize:Zoom(11) weight:0 color:@"#BDBDBD" align:0 text:[NSString stringWithFormat:@"售价: %@元", discount]];
    [view addSubview:bottomL];
    [bottomL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-Zoom(13));
        make.centerX.mas_equalTo(0);
    }];
    
    return view;
}

- (UIView *)payWayWithIndex: (NSInteger)index
{
    NSArray *imgs = @[
        @"walletWechat",
        @"walletAli",
    ];
    NSArray *names = @[
        @"微信",
        @"支付宝",
    ];
    UIView *view = UIView.new;
    [self.payWayViews addObject:view];
    view.tag = index;
    [view rbAddTapGesWithTarget:self action:@selector(payWayViewTapped:)];
    
    UIImageView *imgView = [UIImageView.alloc initWithImage:[UIImage imageNamed:imgs[index]]];
    imgView.contentMode = UIViewContentModeScaleAspectFit;
    [view addSubview:imgView];
    [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(Zoom(16));
        make.width.height.mas_equalTo(Zoom(18));
        make.centerY.mas_equalTo(0);
    }];
    
    UILabel *nameL = [RanTool labelWithFontSize:Zoom(15) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:names[index]];
    [view addSubview:nameL];
    [nameL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(imgView.mas_right).offset(Zoom(9));
        make.centerY.mas_equalTo(0);
    }];
    
    UIImageView *selImgView = UIImageView.new;
    selImgView.tag = 33;
    [view addSubview:selImgView];
    [selImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(-Zoom(16));
        make.width.height.mas_equalTo(Zoom(15));
    }];
    
    return view;
}


- (NSMutableArray *)amountViews
{
    if (!_amountViews)
    {
        self.amountViews = NSMutableArray.array;
    }
    return _amountViews;
}

- (NSMutableArray *)payWayViews
{
    if (!_payWayViews)
    {
        self.payWayViews = NSMutableArray.array;
    }
    return _payWayViews;
}

- (UILabel *)noteL
{
    if (!_noteL)
    {
        self.noteL = ({
            NSString *text = @"1、由于网络原因，提现申请会出现延时处理现象，请在24小时内注意查收\n2、请及时关注零钱，查询是否充值成功\n3、请及时关注余额记录中是否有余额转入\n4、可通过余额明细，查看交易详情\n5、如果充值过程中涉及任何手续费用都由用户承担";
            UILabel *textL = UILabel.new;
            textL.numberOfLines = 0;
            textL.attributedText = [RanTool attributeStringMaker:^(NSMutableArray *maker) {
                [maker addAttributeStringWithFont:[UIFont systemFontOfSize:Zoom(12)] color:[UIColor colorWithHexString:@"#9EA3A9"] string:text lineSpacing:Zoom(7)];
            }];
            textL;
        });
    }
    return _noteL;
}

@end
